package cn.com.huzo.webapp.controller;

import huzofw.websocket.HuzoSocketSessionUtils;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 消息发送控制器
 *
 * @author 吴旭
 */
@Controller
public class MessageController {

    /**
     * 向单个客户端推送消息
     *
     * @param userName 用户名
     * @param userId 用户标识
     * @param message 消息
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "message/send.html")
    public Map<String, Object> sendToOneClient(@RequestParam String userName, @RequestParam Integer userId, @RequestParam String message) {
        Map<String, Object> result = new HashMap();
        try {
            HuzoSocketSessionUtils.sendMessage(userName, userId, message);
            result.put("Success", true);
        } catch (Exception ex) {
            Logger.getLogger(MessageController.class.getName()).log(Level.SEVERE, null, ex);
            result.put("Success", false);
        }

        return result;
    }

    /**
     * 向多个客户端推送消息
     *
     * @param messageData 消息数据（包含发送目标、消息等）
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "message/send.html", method = RequestMethod.POST)
    public Map<String, Object> sendToMultiClient(@RequestBody Map<String, Object> messageData) {
        Map<String, Object> result = new HashMap();
        try {
            //HuzoSocketSessionUtils.sendMessage(userName, userId, message);
            result.put("Success", true);
        } catch (Exception ex) {
            Logger.getLogger(MessageController.class.getName()).log(Level.SEVERE, null, ex);
            result.put("Success", false);
        }

        return result;
    }
}
